package com.kl.forum.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kl.forum.dao.*;
import com.kl.forum.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticlePicMapper articlePicMapper;

    @Autowired
    private CircleArticleMapper circleArticleMapper;

    @Autowired
    private CirclePicMapper circlePicMapper;

    @Autowired
    private ArticleLightMapper articleLightMapper;
    @Autowired
    private UserHealthyMapper userHealthyMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ArticleCommentMapper articleCommentMapper;
    @Autowired
    private ArticleCommentReplyMapper articleCommentReplyMapper;

    public PageInfo<Map> findArticleCollect(Integer userId,int pageNum,int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        Map map =new HashMap();
        map.put("userId",userId);
        List list=articleMapper.findArticleCollect(map);
        PageInfo<Map> pageInfo = new PageInfo<>(list);
        return pageInfo;

    }

    public   List<Article> setPic(List<Article> list){
         List<ArticlePic> articlePicList= articlePicMapper.findPicListByArticleId(list);
        for (int i=0;i<list.size();i++){
            if (articlePicList!=null) {
                List<ArticlePic> circlePics=new ArrayList<>();
                for (int j=0;j<articlePicList.size();j++) {
                    if (articlePicList.get(j) != null) {
                        if (list.get(i).getId().equals(articlePicList.get(j).getArticleId()) ) {
                            circlePics.add(articlePicList.get(j));
                        }
                    }
                    list.get(i).setPicList(circlePics);
                }
            }
//            List<ArticlePic> articlePicList=  articlePicMapper.findPicByArticleId(list.get(i).getId());
//            if (articlePicList!=null){
//                list.get(i).setPicList(articlePicList);
//            }
//            if (!stringRedisTemplate.opsForHash().hasKey("articleViewNumCount",list.get(i).getId().toString())){
//
//            }
//            if (!stringRedisTemplate.opsForHash().hasKey("articleLightNumCount",list.get(i).getId().toString())){
//
//            }
            Object viewNumCount=  stringRedisTemplate.opsForHash().get("articleViewNumCount",list.get(i).getId().toString());
            if (viewNumCount==null){
                stringRedisTemplate.opsForHash().put("articleViewNumCount",list.get(i).getId().toString(),"0");
                list.get(i).setViewNum(0);
            }else{
                list.get(i).setViewNum(Integer.parseInt(viewNumCount.toString()));
            }
            Object lightNumCount=  stringRedisTemplate.opsForHash().get("articleLightNumCount",list.get(i).getId().toString());
            if (lightNumCount==null){
                stringRedisTemplate.opsForHash().put("articleLightNumCount",list.get(i).getId().toString(),"0");
                list.get(i).setLightNum(0);
            }else {
                list.get(i).setLightNum(Integer.parseInt(lightNumCount.toString()));
            }

        }
        return list;
    }

    public PageInfo<Article>  dailySelection(int pageNum,int pageSize ){
        PageHelper.startPage(pageNum, pageSize);
        List<Article> list=articleMapper.dailySelection();
        if (list.size()!=0) {
            list = setPic(list);
        }
        PageInfo<Article> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    public PageInfo<Article> findArticleByColumnId(Integer columnId,Integer pageNum ,Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        Map map=new HashMap();
        map.put("columnId",columnId);
        List<Article> list=articleMapper.findArticle(map);
        if (list.size()!=0){
            list=setPic(list);
        }
        PageInfo<Article> pageInfo = new PageInfo<>(list);
        return pageInfo;

    }
    public PageInfo<Article> findArticleByTagId(Integer tagId,Integer pageNum ,Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        Map map=new HashMap();
        map.put("tagId",tagId);
        List<Article> list=articleMapper.findArticle(map);
        PageInfo<Article> pageInfo = new PageInfo<>(list);
        return pageInfo;

    }
    public PageInfo<Map> findHealthyArticleByUserId(Integer userId,Integer pageNum ,Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        Map map=new HashMap();
        map.put("userId",userId);
        List<Map> list=articleMapper.findHealthyArticleByUserId(map);
        PageInfo<Map> pageInfo = new PageInfo<>(list);
        return pageInfo;

    }
    public PageInfo<Article> findArticleByHealthyNumId(Integer healthyId,Integer pageNum ,Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        Map map=new HashMap();
        map.put("healthyId",healthyId);
        List<Article> list=articleMapper.findArticle(map);
        PageInfo<Article> pageInfo = new PageInfo<>(list);
        return pageInfo;

    }

    public Article articleDetail(int id){
       Article article=  articleMapper.selectByPrimaryKey(id);
       List<ArticlePic> articlePicList=  articlePicMapper.findPicByArticleId(article.getId());
            if (articlePicList!=null){
                article.setPicList(articlePicList);
            }
        return article;
    }

    public int update(Article article){
        return articleMapper.updateByPrimaryKeySelective(article);
    }

    public PageInfo searchArticle(String keyWord, Integer pageNum, Integer pageSize,String type,Integer userId) {
        PageHelper.startPage(pageNum, pageSize);
        Map map=new HashMap();
        map.put("keyWord",keyWord);
        map.put("type",type);
        List<Article> list=articleMapper.searchArticle(map);
        if (list.size()!=0){
            list=setPic(list);
        }

        for (int i=0;i<list.size();i++){
          ArticleLight articleLight= articleLightMapper.findLightRecord(list.get(i).getId(),userId);
          if (articleLight!=null){
              list.get(i).setLightStatus(articleLight.getStatus());
          }
            userHealthyMapper.findUserHealthyBy(list.get(i).getHealthNumId(),userId);
        }
        PageInfo<Article> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    public PageInfo findKindByArticle(Integer kindId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Map map=new HashMap();
        map.put("kindId",kindId);
        List<Article> list=articleMapper.findKindByArticle(map);
//        for (int i=0;i<list.size();i++){
//            List<ArticlePic> articlePicList=  articlePicMapper.findPicByArticleId(list.get(i).getId());
//            if (articlePicList!=null){
//                list.get(i).setPicList(articlePicList);
//            }
//        }
        if (list.size()!=0){
            list=setPic(list);
        }

        PageInfo<Article> pageInfo = new PageInfo<>(list);
        return pageInfo;

    }

    public PageInfo findKindByCircleArticle(Integer kindId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Map map=new HashMap();
        map.put("kindId",kindId);
        List<CircleArticle> list= circleArticleMapper.findKindByCircleArticle(map);
        for (int i=0;i<list.size();i++){
            List<CirclePic> articlePicList=  circlePicMapper.findPicByArticleId(list.get(i).getId());
            if (articlePicList!=null){
                list.get(i).setPicList(articlePicList);
            }
        }
        PageInfo<CircleArticle> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    public PageInfo recommendArticle(Integer pageNum, Integer pageSize,Integer articleId) {
        PageHelper.startPage(pageNum, pageSize);

        List<Article> list=articleMapper.recommendArticle(articleId);
//        for (int i=0;i<list.size();i++){
//            List<ArticlePic> articlePicList=  articlePicMapper.findPicByArticleId(list.get(i).getId());
//            if (articlePicList!=null){
//                list.get(i).setPicList(articlePicList);
//            }
//        }
        if (list.size()!=0) {
            list = setPic(list);
        }
        PageInfo<Article> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


    public int countCommentNum(Integer articleId) {
       int num1= articleCommentMapper.countCommentNum(articleId);
       int num2= articleCommentReplyMapper.countCommentReplyNum(articleId);
       return  num1+num2;

    }
}
